<?php

namespace App\Model;


use Illuminate\Support\Facades\DB;

class Rebate extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'rebate_send_log';
    protected $userTabel = 'user';

    /**
     * 获取返利数据
     */
    public function getRebateList($page, $limit, $user_id, $start_time, $end_time)
    {
        $where = function($q)use($user_id, $start_time, $end_time){
            if(!empty($user_id)){
                $q->where('a.user_id',$user_id);
            }
            if(!empty($start_time) && !empty($end_time)){
                $start_time = strtotime($start_time);
                $start_time = date("Y-m-d",$start_time).' 00:00:00';
                $q->where('a.created_at','>=',$start_time);

                $end_time = strtotime($end_time);
                $end_time = date("Y-m-d",$end_time).' 23:59:59';
                $q->where('a.created_at','<=',$end_time);
            }
        };
        $data = DB::connection($this->connection)
                ->table($this->table.' as a')
                ->leftJoin($this->userTabel.' as b', 'b.user_id','=','a.user_id')
                ->where($where)
                ->orderBy('created_at','DESC')
                ->paginate($limit,['a.id','a.round_time','a.week','a.user_id','b.uuid','b.nickname','a.dd_value','a.all_bag_value','a.all_color_gift_value','a.award_diamond','a.status'],'page',$page);
        $data = objToArray($data);
        return $data;
    }

    public function getSearchAll($user_id, $start_time, $end_time){
        $where = function($q)use($user_id, $start_time, $end_time){
            if(!empty($user_id)){
                $q->where('user_id',$user_id);
            }
            if(!empty($start_time) && !empty($end_time)){
                $start_time = strtotime($start_time);
                $start_time = date("Y-m-d",$start_time).' 00:00:00';
                $q->where('created_at','>=',$start_time);

                $end_time = strtotime($end_time);
                $end_time = date("Y-m-d",$end_time).' 23:59:59';
                $q->where('created_at','<=',$end_time);
            }
        };
        $data = DB::connection($this->connection)
                ->table($this->table)
                ->select(DB::raw("count(*) as total_people,sum(award_diamond) as total_diamond,sum(dd_value) as total_dd,sum(all_bag_value) as total_bag, sum(all_color_gift_value) as total_gift"))
                ->where($where)
                ->first();
        $data = objToArray($data);
        if(!empty($data)){
            $res = DB::connection($this->connection)
                ->table($this->table)
                ->select(DB::raw("count(*) as total_user, sum(award_diamond) as total_award"))
                ->where($where)
                ->where('status', 1)
                ->first();
            $res = objToArray($res); 
        }
        $data['total_user'] = empty($res['total_user']) ? 0 : $res['total_user'];
        $data['total_award'] = empty($res['total_award']) ? 0 : $res['total_award'];
        return $data;
    }
}